﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports System.Security
Imports System.Data.Services.Client
Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient.Credentials

<SecuritySafeCritical()>
Public Class TableServiceContext
    Inherits DataServiceContext
    Implements ITableServiceContext

    Public Sub New(ByVal baseAddress As String, ByVal credentials As IStorageCredentials)
        MyBase.New(New Uri(baseAddress))
        If String.IsNullOrEmpty(baseAddress) Then
            Throw New ArgumentNullException("baseAddress")
        End If

        AddHandler Me.SendingRequest, AddressOf DataContextSendingRequest
        Me.IgnoreMissingProperties = True
        Me.MergeOption = MergeOption.PreserveChanges

        Me.StorageCredentials = credentials
    End Sub

    Private Custom Event InnerReadingEntity As EventHandler(Of ReadingWritingEntityEventArgs) Implements ITableServiceContext.ReadingEntity
        AddHandler(ByVal value As EventHandler(Of ReadingWritingEntityEventArgs))
            AddHandler MyBase.ReadingEntity, value
        End AddHandler

        RemoveHandler(ByVal value As EventHandler(Of ReadingWritingEntityEventArgs))
            RemoveHandler MyBase.ReadingEntity, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        End RaiseEvent
    End Event

    Private Custom Event InnerSendingRequest As EventHandler(Of SendingRequestEventArgs) Implements ITableServiceContext.SendingRequest
        AddHandler(ByVal value As EventHandler(Of SendingRequestEventArgs))
            AddHandler MyBase.SendingRequest, value
        End AddHandler

        RemoveHandler(ByVal value As EventHandler(Of SendingRequestEventArgs))
            RemoveHandler MyBase.SendingRequest, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        End RaiseEvent
    End Event

    Private Custom Event InnerWritingEntity As EventHandler(Of ReadingWritingEntityEventArgs) Implements ITableServiceContext.WritingEntity
        AddHandler(ByVal value As EventHandler(Of ReadingWritingEntityEventArgs))
            AddHandler MyBase.WritingEntity, value
        End AddHandler

        RemoveHandler(ByVal value As EventHandler(Of ReadingWritingEntityEventArgs))
            RemoveHandler MyBase.WritingEntity, value
        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
        End RaiseEvent
    End Event

    Public Property StorageCredentials() As IStorageCredentials Implements ITableServiceContext.StorageCredentials

    Private Property InnerUsePostTunneling As Boolean Implements ITableServiceContext.UsePostTunneling
        Get
            Return MyBase.UsePostTunneling
        End Get
        Set(ByVal value As Boolean)
            MyBase.UsePostTunneling = value
        End Set
    End Property

    Private Property InnerTypeScheme As Uri Implements ITableServiceContext.TypeScheme
        Get
            Return MyBase.TypeScheme
        End Get
        Set(ByVal value As Uri)
            MyBase.TypeScheme = value
        End Set
    End Property

    Private Property InnerIgnoreMissingProperties As Boolean Implements ITableServiceContext.IgnoreMissingProperties
        Get
            Return MyBase.IgnoreMissingProperties
        End Get
        Set(ByVal value As Boolean)
            MyBase.IgnoreMissingProperties = value
        End Set
    End Property

    Private Property InnerIgnoreResourceNotFoundException As Boolean Implements ITableServiceContext.IgnoreResourceNotFoundException
        Get
            Return MyBase.IgnoreResourceNotFoundException
        End Get
        Set(ByVal value As Boolean)
            MyBase.IgnoreResourceNotFoundException = value
        End Set
    End Property

    Private ReadOnly Property InnerLinks As Collections.ObjectModel.ReadOnlyCollection(Of LinkDescriptor) Implements ITableServiceContext.Links
        Get
            Return MyBase.Links
        End Get
    End Property

    Private Property InnerMergeOption As MergeOption Implements ITableServiceContext.MergeOption
        Get
            Return MyBase.MergeOption
        End Get
        Set(ByVal value As MergeOption)
            MyBase.MergeOption = value
        End Set
    End Property

    Private Property InnerResolveName As Func(Of Type, String) Implements ITableServiceContext.ResolveName
        Get
            Return MyBase.ResolveName
        End Get
        Set(ByVal value As Func(Of Type, String))
            MyBase.ResolveName = value
        End Set
    End Property

    Private Property InnerResolveType As Func(Of String, Type) Implements ITableServiceContext.ResolveType
        Get
            Return MyBase.ResolveType
        End Get
        Set(ByVal value As Func(Of String, Type))
            MyBase.ResolveType = value
        End Set
    End Property

    Private Property InnerSaveChangesDefaultOptions As SaveChangesOptions Implements ITableServiceContext.SaveChangesDefaultOptions
        Get
            Return MyBase.SaveChangesDefaultOptions
        End Get
        Set(ByVal value As SaveChangesOptions)
            MyBase.SaveChangesDefaultOptions = value
        End Set
    End Property

    Private Property InnerDataNamespace As String Implements ITableServiceContext.DataNamespace
        Get
            Return MyBase.DataNamespace
        End Get
        Set(ByVal value As String)
            MyBase.DataNamespace = value
        End Set
    End Property

    Private ReadOnly Property InnerApplyingChanges As Boolean Implements ITableServiceContext.ApplyingChanges
        Get
            Return MyBase.ApplyingChanges
        End Get
    End Property

    Private ReadOnly Property InnerBaseUri As Uri Implements ITableServiceContext.BaseUri
        Get
            Return MyBase.BaseUri
        End Get
    End Property

    Private ReadOnly Property InnerEntities As Collections.ObjectModel.ReadOnlyCollection(Of EntityDescriptor) Implements ITableServiceContext.Entities
        Get
            Return MyBase.Entities
        End Get
    End Property

    Private Sub AddTable(ByVal table As TableServiceSchema) Implements ITableServiceContext.AddTable
        If table Is Nothing Then
            Throw New ArgumentNullException("table")
        End If

        If String.IsNullOrEmpty(table.TableName) Then
            Throw New ArgumentException("You need to provide a valid name for the new table", "table")
        End If

        Me.AddObject("Tables", table)
    End Sub

    Private Sub InnerAddObject(ByVal entitySetName As String, ByVal entity As Object) Implements ITableServiceContext.AddObject
        MyBase.AddObject(entitySetName, entity)
    End Sub

    Private Sub InnerAddRelatedObject(ByVal source As Object, ByVal sourceProperty As String, ByVal target As Object) Implements ITableServiceContext.AddRelatedObject
        MyBase.AddRelatedObject(source, sourceProperty, target)
    End Sub

    Private Sub InnerAttachTo(ByVal entitySetName As String, ByVal entity As Object) Implements ITableServiceContext.AttachTo
        MyBase.AttachTo(entitySetName, entity)
    End Sub

    Private Sub InnerAttachTo(ByVal entitySetName As String, ByVal entity As Object, ByVal etag As String) Implements ITableServiceContext.AttachTo
        MyBase.AttachTo(entitySetName, entity, etag)
    End Sub

    Private Function InnerBeginExecute(Of T)(ByVal continuation As DataServiceQueryContinuation(Of T), ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginExecute
        Return MyBase.BeginExecute(Of T)(continuation, callback, state)
    End Function

    Private Function InnerBeginExecute(Of TElement)(ByVal requestUri As Uri, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginExecute
        Return MyBase.BeginExecute(Of TElement)(requestUri, callback, state)
    End Function

    Private Function InnerBeginExecuteBatch(ByVal callback As AsyncCallback, ByVal state As Object, ByVal ParamArray queries() As DataServiceRequest) As IAsyncResult Implements ITableServiceContext.BeginExecuteBatch
        Return MyBase.BeginExecuteBatch(callback, state, queries)
    End Function

    Private Function InnerBeginGetReadStream(ByVal entity As Object, ByVal args As DataServiceRequestArgs, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginGetReadStream
        Return MyBase.BeginGetReadStream(entity, args, callback, state)
    End Function

    Private Function InnerBeginLoadProperty(ByVal entity As Object, ByVal propertyName As String, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginLoadProperty
        Return MyBase.BeginLoadProperty(entity, propertyName, callback, state)
    End Function

    Private Function InnerBeginLoadProperty(ByVal entity As Object, ByVal propertyName As String, ByVal continuation As DataServiceQueryContinuation, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginLoadProperty
        Return MyBase.BeginLoadProperty(entity, propertyName, continuation, callback, state)
    End Function

    Private Function InnerBeginLoadProperty(ByVal entity As Object, ByVal propertyName As String, ByVal nextLinkUri As Uri, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginLoadProperty
        Return MyBase.BeginLoadProperty(entity, propertyName, nextLinkUri, callback, state)
    End Function

    Private Function InnerBeginSaveChanges(ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginSaveChanges
        Return MyBase.BeginSaveChanges(callback, state)
    End Function

    Private Function InnerBeginSaveChanges(ByVal options As SaveChangesOptions, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult Implements ITableServiceContext.BeginSaveChanges
        Return MyBase.BeginSaveChanges(options, callback, state)
    End Function

    Private Sub InnerCancelRequest(ByVal asyncResult As IAsyncResult) Implements ITableServiceContext.CancelRequest
        MyBase.CancelRequest(asyncResult)
    End Sub

    Private Sub InnerDeleteObject(ByVal entity As Object) Implements ITableServiceContext.DeleteObject
        MyBase.DeleteObject(entity)
    End Sub

    Private Function InnerDetach(ByVal entity As Object) As Boolean Implements ITableServiceContext.Detach
        Return MyBase.Detach(entity)
    End Function

    Private Function InnerEndExecute(Of TElement)(ByVal asyncResult As IAsyncResult) As Collections.Generic.IEnumerable(Of TElement) Implements ITableServiceContext.EndExecute
        Return MyBase.EndExecute(Of TElement)(asyncResult)
    End Function

    Private Function InnerEndExecuteBatch(ByVal asyncResult As IAsyncResult) As DataServiceResponse Implements ITableServiceContext.EndExecuteBatch
        Return MyBase.EndExecuteBatch(asyncResult)
    End Function

    Private Function InnerEndGetReadStream(ByVal asyncResult As IAsyncResult) As DataServiceStreamResponse Implements ITableServiceContext.EndGetReadStream
        Return MyBase.EndGetReadStream(asyncResult)
    End Function

    Private Function InnerEndLoadProperty(ByVal asyncResult As IAsyncResult) As QueryOperationResponse Implements ITableServiceContext.EndLoadProperty
        Return MyBase.EndLoadProperty(asyncResult)
    End Function

    Private Function InnerEndSaveChanges(ByVal asyncResult As IAsyncResult) As DataServiceResponse Implements ITableServiceContext.EndSaveChanges
        Return MyBase.EndSaveChanges(asyncResult)
    End Function

    Private Function InnerGetEntityDescriptor(ByVal entity As Object) As EntityDescriptor Implements ITableServiceContext.GetEntityDescriptor
        Return MyBase.GetEntityDescriptor(entity)
    End Function

    Private Function InnerGetMetadataUri() As Uri Implements ITableServiceContext.GetMetadataUri
        Return MyBase.GetMetadataUri()
    End Function

    Private Function InnerGetReadStreamUri(ByVal entity As Object) As Uri Implements ITableServiceContext.GetReadStreamUri
        Return MyBase.GetReadStreamUri(entity)
    End Function

    Private Sub InnerSetSaveStream(ByVal entity As Object, ByVal stream As IO.Stream, ByVal closeStream As Boolean, ByVal contentType As String, ByVal slug As String) Implements ITableServiceContext.SetSaveStream
        MyBase.SetSaveStream(entity, stream, closeStream, contentType, slug)
    End Sub

    Private Sub InnerSetSaveStream(ByVal entity As Object, ByVal stream As IO.Stream, ByVal closeStream As Boolean, ByVal args As DataServiceRequestArgs) Implements ITableServiceContext.SetSaveStream
        MyBase.SetSaveStream(entity, stream, closeStream, args)
    End Sub

    Private Function InnerTryGetEntity(Of TEntity As Class)(ByVal identity As Uri, ByRef entity As TEntity) As Boolean Implements ITableServiceContext.TryGetEntity
        Return MyBase.TryGetEntity(Of TEntity)(identity, entity)
    End Function

    Private Function InnerTryGetUri(ByVal entity As Object, ByRef identity As Uri) As Boolean Implements ITableServiceContext.TryGetUri
        Return MyBase.TryGetUri(entity, identity)
    End Function

    Private Sub InnerUpdateObject(ByVal entity As Object) Implements ITableServiceContext.UpdateObject
        MyBase.UpdateObject(entity)
    End Sub

    Private Sub DataContextSendingRequest(ByVal sender As Object, ByVal e As SendingRequestEventArgs)
        Me.StorageCredentials.AddAuthenticationHeadersLite(e.RequestData, e.RequestHeaders)
    End Sub
End Class